using IRepository;
using IServices;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using YL.Core.Dto;
using YL.Core.Entity;
using YL.Core.Entity.Meter;
using YL.Utils.Extensions;
using YL.Utils.Json;
using YL.Utils.Pub;
using YL.Utils.Table;

namespace Services
{
    public class Meter_CameraServices : BaseServices<Camera>, IMeter_CameraServices
    {
        private readonly IMeter_CameraRepository _repository;
        private readonly ISys_rolemenuServices _rolemenuServices;
        private readonly ISys_menuServices _menuServices;
        private readonly SqlSugarClient _client;

        public Meter_CameraServices(ISys_menuServices menuServices, ISys_rolemenuServices rolemenuServices, SqlSugarClient client, IMeter_CameraRepository repository) : base(repository)
        {
            _repository = repository;
            _client = client;
            _rolemenuServices = rolemenuServices;
            _menuServices = menuServices;
        }

        public string PageList(Bootstrap.BootstrapParams bootstrap)
        {
            int totalNumber = 0;
            if (bootstrap.offset != 0)
            {
                bootstrap.offset = bootstrap.offset / bootstrap.limit + 1;
            }

            var query = _client.Queryable<Camera, DeviceInfo, AllLevelUserClientInfoView>
                ((s, w, c) => new object[] {
                   JoinType.Left,s.DeviceId==w.DeviceId,
                    JoinType.Left,s.ClientId==w.ClientId,
                     JoinType.Left,s.ClientId==c.ClientId,
                     JoinType.Left,w.ClientId==c.ClientId
                 })
                 .Where((s, w, c) => c.UserId == bootstrap.UserId && s.IsDel==1 && s.DeviceId==w.DeviceId && s.ClientId==w.ClientId && s.ClientId==c.ClientId)
                 .Select((s, w, c) => new
                 {
                     ClientId = s.ClientId.ToString(),
                     c.ClientName,
                     DeviceId =s.DeviceId.ToString(),
                     w.DeviceName,
                     CameraId = s.CameraId.ToString(),
                     s.CameraName,
                     s.AccessToken,
                     s.VideoURL,
                     s.ChannelNo,
                     s.CaptureTime,
                     s.CreateDate,
                     s.Remark
                 }).MergeTable();

            if (!bootstrap.search.IsEmpty())
            {
                query.Where((s) => s.DeviceName.Contains(bootstrap.search));
            }

            if (!bootstrap.datemin.IsEmpty() && !bootstrap.datemax.IsEmpty())
            {
                query.Where(s => s.CreateDate > bootstrap.datemin.ToDateTimeB() && s.CreateDate <= bootstrap.datemax.ToDateTimeE());
            }
            if (bootstrap.order.Equals("desc", StringComparison.OrdinalIgnoreCase))
            {
                query.OrderBy($"MergeTable.{bootstrap.sort} desc");
            }
            else
            {
                query.OrderBy($"MergeTable.{bootstrap.sort} asc");
            }
            var list = query.ToPageList(bootstrap.offset, bootstrap.limit, ref totalNumber);
            return Bootstrap.GridData(list, totalNumber).JilToJson();
        }
    }
}